package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.web.common.shared.HttpStatus;
import com.mbc.common.entities.Branch;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.CommonMessage;
import com.mbc.web.common.client.ViewUtils;
import com.mbc.web.common.client.ui.control.GridLabelColumn;
import com.mbc.web.common.client.ui.control.GridView;
import com.mbc.web.common.client.ui.control.MButton;
import com.mbc.web.common.client.ui.control.Pager;
import com.mbc.web.common.shared.dto.BranchDataListDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;

public class SearchBranch extends AbstractListView<Branch> {

	@UiField GridView gvDisplay;
	@UiField Button btnSearch;
	@UiField Pager pager;
	@UiField TextBox txtKeyword;
	@UiField MButton btnAddNew;
	@UiField ListBox lstfield;
	
	private String l_branchID;
	
	public static final byte SAVE = 1;
	public static final byte DELETE = 0;
	
	private GridLabelColumn colBranchCode = new GridLabelColumn();
	private GridLabelColumn colEmail = new GridLabelColumn();
	private GridLabelColumn colPhone = new GridLabelColumn();
	private GridLabelColumn colName = new GridLabelColumn();
	private GridLabelColumn colCity = new GridLabelColumn();
	
	private static SearchBranchUiBinder uiBinder = GWT
			.create(SearchBranchUiBinder.class);

	interface SearchBranchUiBinder extends UiBinder<Widget, SearchBranch> {
	}

	public SearchBranch() {
		initWidget(uiBinder.createAndBindUi(this));
		l_branchID="";
		initialize();
	}
	public SearchBranch(String message, byte status){
		initWidget(uiBinder.createAndBindUi(this));
		l_branchID="";
		if(status == SAVE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.saved_message);
		else if(status ==  DELETE)
			ViewUtils.showSuccessMessage(message + " " + CommonMessage.deleted_message);
		initialize();
	}
    //MBC(11/8/2014)
	public SearchBranch(String title,String status) {
		initWidget(uiBinder.createAndBindUi(this));
		this.title = title;
		if(!status.equals("")){
			l_branchID = status;
		}else
			l_branchID = "";
		initialize();
	}
	//End
	@Override
	protected void initialize() {
		
		lstfield.addItem("Branch Code","BranchCode");
		lstfield.addItem("Name","Name");
		lstfield.addItem("Phone","Phone");
		lstfield.addItem("Email","Email");
		lstfield.addItem("City","City");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				txtKeyword.setFocus(true);
			}
		});
		colBranchCode.header = "Branch Code";
		colBranchCode.styleName = "clickable-col";
		colBranchCode.clickHandler = new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				//MBC Begin
				if(isModal){
					selectedEntity = dataList.get(gvDisplay.getSelectedRowIndex());
					closeModal();
				}
				else	// MBC End
				ClientSingleton.changeView(new FrmBranch(dataList.get(gvDisplay.getSelectedRowIndex())));
			}
		};
		gvDisplay.addColumn(colBranchCode);
		
		colName.header = "Name";
		gvDisplay.addColumn(colName);
		
		colPhone.header = "Phone";
		gvDisplay.addColumn(colPhone);
		
		colEmail.header = "Email";
		gvDisplay.addColumn(colEmail);
		
		colCity.header = "City";
		gvDisplay.addColumn(colCity);
		
		pager.btnFirst_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnPrevious_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnNext_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		pager.btnLast_ClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				search();
			}
		});
		
		onBtnSearchClick(null);
	}

	@Override
	protected void bindDisplay(List<Branch> list) {
		gvDisplay.removeAllRows();
		for (Branch l_Branch : list) {
			gvDisplay.addRow(new String[]{
					l_Branch.getBranchCode(), 
					l_Branch.getName(),
					l_Branch.getPhone(),
					l_Branch.getEmail(),
					l_Branch.getCity()});
		}
		generateList();
	}

	@Override
	protected void search() {
		ViewUtils.startAjaxLoader(CommonMessage.loading_message);
		CommonControllerMgr.findBranches(prepareCri(), findBranchesCallback());
	}
	
	private AsyncCallback<BranchDataListDTO> findBranchesCallback(){
		return new AsyncCallback<BranchDataListDTO>() {
			
			@Override
			public void onSuccess(BranchDataListDTO result) {
				ViewUtils.stopAjaxLoader();
				if(result.getHttpStatus()==HttpStatus.OK){
					dataList = result.getBranchDataList().getEntityList();
					pager.totalRecord = result.getBranchDataList().getTotalRecord();
					bindDisplay(dataList);
					pager.prepareVisible();
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}

	private Branch prepareCri(){
		Branch l_cri = new Branch();
		l_cri.setKeyword(txtKeyword.getValue());
		l_cri.setIndex(pager.index);
		l_cri.setSize(10);
		if(!l_branchID.equals(""))
			l_cri.setStatus("true");
		else
			l_cri.setStatus("false");
		l_cri.setFieldname(lstfield.getValue(lstfield.getSelectedIndex()));
		return l_cri;
	}
	
	@UiHandler("btnSearch")
	void onBtnSearchClick(ClickEvent event) {
		pager.index = 0;
		search();
	}
	
	@UiHandler("btnAddNew")
	void onBtnAddNewClick(ClickEvent event) {
		ClientSingleton.changeView(new FrmBranch());
	}
	
	@UiHandler("txtKeyword")
	void onTxtKeywordKeyDown(KeyDownEvent event) {
		if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
			onBtnSearchClick(null);
	}
}
